using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Liberty.Application.Kernel;
using Liberty.Domain.Kernel;
using Liberty.Domain.ServiceMemberManagement;

using Peradigm.Framework.Domain.Parties;
using Peradigm.Framework.Persistence;
using Peradigm.Framework.UI.Web;

//namespace Liberty.UI.Kernel.Controls
//{

public partial class AddressView : AbstractLibertyControl
{
    #region Constants
    public const string
        OID = "Oid",
        DESCRIPTION = "Description",
        DEFAULT_ITEM = "Select a state",
        SKIN_REQUIRED_FIELD = "RequiredField";
    private const long
        DEFAULT_ITEM_VALUE = 0;
    #endregion

    #region Event Handlers
    protected void Page_Load( object sender, EventArgs e )
    {
        if( !this.IsPostBack )
        {
            Liberty.Domain.Kernel.Address address = (Liberty.Domain.Kernel.Address)this.Model;

            ReferenceValue state = null;
            if( address != null )
            {
                this.txtAddress1.Text = address.DeliveryAddress;
                this.txtAddress2.Text = address.SecondaryUnit;
                this.txtCity.Text = address.City;
                this.txtZip.Text = address.PostalCode;
                state = this.AddressController.StateWith( address.Province );
            }

            this.BindList( this.ddlState,
                           this.AddressController.AllStates(),
                           state );
        }
    }

    public override void UpdateView()
    {
        base.UpdateView();
        Liberty.Domain.Kernel.Address address = (Liberty.Domain.Kernel.Address)this.Model;

        ReferenceValue state = null;
        if( address != null )
        {
            this.txtAddress1.Text = address.DeliveryAddress;
            this.txtAddress2.Text = address.SecondaryUnit;
            this.txtCity.Text = address.City;
            this.txtZip.Text = address.PostalCode;
            state = this.AddressController.StateWith( address.Province );
        }

        this.BindList( this.ddlState,
                       this.AddressController.AllStates(),
                       state );
    }

    public override void UpdateModel()
    {
        Liberty.Domain.Kernel.Address address = (Liberty.Domain.Kernel.Address)this.Model;

        address.DeliveryAddress = this.txtAddress1.Text;
        address.SecondaryUnit = this.txtAddress2.Text;
        address.City = this.txtCity.Text;
        address.PostalCode = this.txtZip.Text;

        address.Province = this.ddlState.SelectedIndex > 0
            ? this.ddlState.SelectedItem.Text
            : String.Empty;
    }
    #endregion

    #region Methods
    #endregion

    #region Properties
    [Browsable( true )]
    public string Label
    {
        get
        {
            return this.lblAddressType.Text;
        }
        set
        {
            this.lblAddressType.Text = value;
        }
    }

    public override object Model
    {
        get
        {
            if( base.Model == null )
            {
                base.Model = this.Session[this.ID + ".Model"];
            }
            return base.Model;
        }
        set
        {
            base.Model = value;
            this.Session[this.ID + ".Model"] = base.Model;
        }
    }

    [Browsable( true )]
    public bool Required
    {
        get
        {
            return i_Required;
        }
        set
        {
            i_Required = value;
            this.ToggleValidationControls( value );
        }
    }
    #endregion

    #region Private Methods
    private void ToggleValidationControls( bool required )
    {
        this.rfvAddress1.Visible = required;
        this.rfvCity.Visible = required;
        this.rfvState.Visible = required;
        this.rfvZip.Visible = required;
        if( required )
        {
            this.lblAddressType.SkinID = SKIN_REQUIRED_FIELD;
            this.lblCity.SkinID = SKIN_REQUIRED_FIELD;
            this.lblState.SkinID = SKIN_REQUIRED_FIELD;
            this.lblZip.SkinID = SKIN_REQUIRED_FIELD;
        }
    }

    private void BindList( DropDownList control, IEnumerable items, ReferenceValue model )
    {
        control.Items.Clear();

        control.DataSource = items;
        control.DataValueField = OID;
        control.DataTextField = DESCRIPTION;
        control.DataBind();
        control.Items.Insert( 0, new ListItem( DEFAULT_ITEM, DEFAULT_ITEM_VALUE.ToString() ) );

        control.ClearSelection();
        if( model != null )
        {
            ListItem selectableItem = control.Items.FindByValue( model.Oid.ToString() );
            if( selectableItem != null )
            {
                selectableItem.Selected = true;
            }
        }
    }
    #endregion

    #region Private Properties
    private AddressController AddressController
    {
        get
        {
            return this.i_AddressController;
        }
    }
    #endregion

    #region Construction and Finalization
    #endregion

    #region Data Elements
    private AddressController i_AddressController = new AddressController();
    private bool i_Required;
    #endregion
}
//}
